﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Gollie.Mail
{
	public static class MailClient
	{
		private static ChannelFactory<IMailServiceChannel> MailChannelFactory = new ChannelFactory<IMailServiceChannel>("GollieMailService");
		private const string endpointNotFoundText = "De Gollie mail dienst is op dit moment niet bereikbaar. Probeert u het alstublieft later nog eens.";

		public static void Queue(EMail email)
		{
			try
			{
				var mailServiceChannel = MailChannelFactory.CreateChannel();
				mailServiceChannel.Open();

				mailServiceChannel.QueueAsync(email);

				mailServiceChannel.Close();
			}
			catch (EndpointNotFoundException)
			{
				throw new Exception(endpointNotFoundText);
			}
		}

		public static void Queue(EMailCollection emails)
		{
			try
			{
				var mailServiceChannel = MailChannelFactory.CreateChannel();
				mailServiceChannel.Open();

				mailServiceChannel.QueueCollectionAsync(emails);

				mailServiceChannel.Close();
			}
			catch (EndpointNotFoundException)
			{
				throw new Exception(endpointNotFoundText);
			}
		}

		public static void Queue(BulkMail emails)
		{
			try
			{
				var mailServiceChannel = MailChannelFactory.CreateChannel();
				mailServiceChannel.Open();

				mailServiceChannel.QueueBulkAsync(emails);

				mailServiceChannel.Close();
			}
			catch (EndpointNotFoundException)
			{
				throw new Exception(endpointNotFoundText);
			}
		}
	}
}
